# -*- coding: utf-8 -*-
"""
@Time: 2025/3/24 14:25
@Author: jins
@File: api_client.py
@Introduction: 
"""
import time
from loguru import logger as log
import requests


class APIClient:
    def __init__(self, base_url):
        self.base_url = base_url

    def send_request(self, endpoint, method='post', headers=None, params=None, data=None, timeout=10):
        url = self.base_url + endpoint
        response = None
        try:
            if method.lower() == 'get':
                response = requests.get(url, headers=headers, params=params)
            elif method.lower() == 'post':
                print('\n' + '=' * 190)
                print(f"\n开始通过http发送请求，接口地址为：{url}，等待超时时间为{timeout}s")
                print('-' * 190)
                print(f"请求结构体为：{str(data).replace("'", '"')}")
                print('-' * 190)
                response = requests.post(url, headers=headers, json=data, timeout=timeout)
                print(f"http返回信息为：{response.text}")
            return response
        except requests.RequestException as e:
            print(f"请求发生错误: {e}")
            return None
        finally:
            time.sleep(1)
